/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductEditView.java
 *
 * Created on 17/10/2009, 10:07:26 AM
 */

package javaclientcp3110.View.Product;

import common.Objects.Component;
import common.Objects.Product;
import common.UCM.UC2AL;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;
import javaclientcp3110.Model.Product.ProductEditModel;
import javaclientcp3110.UCM.UCEditProduct;
import javaclientcp3110.View.Component.ComponentListView;
import javax.swing.JOptionPane;

/**
 *
 * @author Slasher
 */
public class ProductEditView extends javax.swing.JPanel {
    ProductEditModel model;

    /** Creates new form ProductEditView */
    public ProductEditView() {
        this (new ProductEditModel());
    }
    public ProductEditView(ProductEditModel model) {
        initComponents();
        this.model = model;

        productListView.updateProductView();
        productListView.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                updateCurrentProduct();
            }
        });
        updateCurrentProduct();
        prodEditButton.addActionListener(new UC2AL(new UCEditProduct(this, model)));
    }
    public String getProdName(){return prodNameTextField.getText();}
    public String getProdDescription(){return prodDescriptionTextField.getText();}
    public double getProdPrice(){return Double.parseDouble(prodRetailPriceTextField.getText());}
    public int getProdId() {
        if (productListView.getItemCount() > 0) {
            Product p = (Product) productListView.getSelectedItem();
            return p.getId();
        }
        else {
            return -1;
        }
    }
    private void updateCurrentProduct() {
        if (productListView.getItemCount() > 0) {
            Product currentItem = (Product)productListView.getSelectedItem();
            prodNameTextField.setText(currentItem.getName());
            prodDescriptionTextField.setText(currentItem.getDescription());
            productComponentList.createList(currentItem.getComponents());
            repaint();
            for (ComponentListView clv : productComponentList.getComponentView()) {
                clv.addItemListener(new ItemListener() {
                    public void itemStateChanged(ItemEvent e) {
                        updateBasePrice();
                    }
                });
            }
            updateBasePrice();
            prodRetailPriceTextField.setText(String.valueOf(currentItem.getPrice()));
        }
        else {
            prodNameTextField.setText("");
            prodDescriptionTextField.setText("");
            productComponentList.createList(null);
            prodRetailPriceTextField.setText("");
            prodRetailPriceTextField.setText(String.valueOf(0.0));
        }
    }
    private void updateBasePrice() {
        double basePrice = 0.0;
        for (ComponentListView clv : productComponentList.getComponentView()) {
            Component c = (Component) clv.getSelectedItem();
            basePrice += c.getPrice();
        }
        prodBasePriceTextField.setText(String.valueOf(basePrice));
    }
    public Vector<Component> getProdComponents() {
        Vector<Component> components = new Vector<Component>();
        for (ComponentListView clv : productComponentList.getComponentView()){
            components.add((Component)clv.getSelectedItem());
        }
        return components;
    }
    public void errorMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Error",
            JOptionPane.ERROR_MESSAGE);
    }
    public void successMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Success!",
            JOptionPane.INFORMATION_MESSAGE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        productComponentList = new javaclientcp3110.View.Product.ComponentsListedViaCatagorysView();
        prodRetailPriceLabel = new javax.swing.JLabel();
        prodDescriptionTextField = new javax.swing.JTextField();
        prodDescriptionLabel = new javax.swing.JLabel();
        prodNameTextField = new javax.swing.JTextField();
        prodNameLabel = new javax.swing.JLabel();
        prodBasePriceTextField = new javax.swing.JTextField();
        prodBasePriceLabel = new javax.swing.JLabel();
        prodEditButton = new javax.swing.JButton();
        prodRetailPriceTextField = new javax.swing.JTextField();
        titleLabel = new javax.swing.JLabel();
        productListView = new javaclientcp3110.View.Product.ProductListView();
        prodProductLabel = new javax.swing.JLabel();

        productComponentList.setMaximumSize(new java.awt.Dimension(219, 87));

        javax.swing.GroupLayout productComponentListLayout = new javax.swing.GroupLayout(productComponentList);
        productComponentList.setLayout(productComponentListLayout);
        productComponentListLayout.setHorizontalGroup(
            productComponentListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 219, Short.MAX_VALUE)
        );
        productComponentListLayout.setVerticalGroup(
            productComponentListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 87, Short.MAX_VALUE)
        );

        prodRetailPriceLabel.setText("Retail Price $");

        prodDescriptionTextField.setColumns(25);
        prodDescriptionTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        prodDescriptionLabel.setText("Description:");

        prodNameTextField.setColumns(25);
        prodNameTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        prodNameLabel.setText("Name:");

        prodBasePriceTextField.setColumns(25);
        prodBasePriceTextField.setEditable(false);
        prodBasePriceTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        prodBasePriceLabel.setText("Base Price $");

        prodEditButton.setText("Edit Product");

        prodRetailPriceTextField.setColumns(25);
        prodRetailPriceTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        titleLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        titleLabel.setText("Select a product to edit then change the details");

        productListView.setMaximumSize(new java.awt.Dimension(28, 20));

        prodProductLabel.setText("Product:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(prodBasePriceLabel)
                                    .addComponent(prodRetailPriceLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(prodRetailPriceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(prodBasePriceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                                    .addComponent(prodEditButton, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(productComponentList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(prodProductLabel)
                                .addComponent(prodDescriptionLabel)
                                .addComponent(prodNameLabel))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(prodDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(productListView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(prodNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(titleLabel)))
                .addGap(97, 97, 97))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(productListView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodProductLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodDescriptionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(productComponentList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodBasePriceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(prodBasePriceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(prodRetailPriceLabel)
                    .addComponent(prodRetailPriceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(prodEditButton)
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel prodBasePriceLabel;
    private javax.swing.JTextField prodBasePriceTextField;
    private javax.swing.JLabel prodDescriptionLabel;
    private javax.swing.JTextField prodDescriptionTextField;
    private javax.swing.JButton prodEditButton;
    private javax.swing.JLabel prodNameLabel;
    private javax.swing.JTextField prodNameTextField;
    private javax.swing.JLabel prodProductLabel;
    private javax.swing.JLabel prodRetailPriceLabel;
    private javax.swing.JTextField prodRetailPriceTextField;
    private javaclientcp3110.View.Product.ComponentsListedViaCatagorysView productComponentList;
    private javaclientcp3110.View.Product.ProductListView productListView;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

}
